import {defineConfig, loadEnv} from 'vite'
import path from 'path';
import autoImport from "unplugin-auto-import/vite";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import {ElementPlusResolver} from "unplugin-vue-components/resolvers";

import vue from '@vitejs/plugin-vue'
import {viteMockServe} from "vite-plugin-mock";

const CWD = process.cwd();
const pathSrc = path.resolve(__dirname, 'src')
console.log(" CWD -->", CWD)
// https://cn.vitejs.dev/
export default defineConfig(({command, mode, isSsrBuild, isPreview}) => {
    const {VITE_BASE, VITE_API_URL} = loadEnv(mode, CWD);
    const env = loadEnv(mode, CWD)

    return {
        base: VITE_BASE,
        server: {
            port: 7000,
            host: true,
            open: true,
            proxy: {
                '/dev-api': {
                    target: VITE_API_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/dev-api/, ''),
                },
            },
        },
        //文件引用路径别名
        resolve: {
            alias: {
                '@': path.resolve(__dirname, './src'),
                '~': path.resolve(__dirname, './'),
            },
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
        },
        // css预处理器
        // css: {
        //     preprocessorOptions: {
        // scss: {
        //     javascriptEnabled: true,
        //     additionalData: `@import"./src/common/style/setting.scss";`,
        // }

        // less: {
        //     modifyVars: {
        //         hack: `true; @import (reference) "${path.resolve('src/assets/style/abl-flex.less')}";`,
        //     },
        //     math: 'strict',
        //     javascriptEnabled: true,
        // },
        // },
        // },
        // 插件配置
        plugins: [
            vue(),
            autoImport({
                imports: [
                    'vue',
                    'vue-router',
                    'pinia'
                ],
                dts: true
            }),
            //
            //  ElementPlus
            AutoImport({resolvers: [ElementPlusResolver()],}),
            Components({resolvers: [ElementPlusResolver()],}),
            //
            //Mock
            viteMockServe({
                enable: env.VITE_MOCK_OPEN === "true",
                supportTs: true, // 是否开启支持ts
                mockPath: 'mock', // 设置mockPath为根目录下的mock目录
                localEnabled: true, // 设置是否监视mockPath对应的文件夹内文件中的更改
                prodEnabled: false, // 设置是否启用生产环境的mock服务
                watchFiles: true, // 是否监视文件更改
                logger: true  //是否在控制台显示请求日志
            })

        ],
    };
})

